﻿using System;
using System.Drawing;
using System.Drawing.Imaging;

namespace ru.yourpictures.Core.Imaging.Filters
{
    public class _BitmapInfo
    {
        #region Ctor
        public _BitmapInfo(Bitmap b) 
        {
            _b = b;

            // Lock bitmap bits
            BitmapData _bmpData = b.LockBits(
                new Rectangle(0, 0, b.Width, b.Height),
                ImageLockMode.ReadWrite, b.PixelFormat);

            // Pointer to Bitmap first byte in memory
            IntPtr _ptr = _bmpData.Scan0;

            // Stride
            _stride = _bmpData.Stride;

            // Pixels per row
            _width = b.Width;

            // Pixels per column
            _height = b.Height;

            // Total count of bytes in image
            _size = _stride * _height;

            // How many bytes each pixel takes
            _bytesPerPixel = Image.GetPixelFormatSize(b.PixelFormat) / 8;

            // Create memory arry
            _pixels = new byte[_size];

            // Copy data from image into memory array
            System.Runtime.InteropServices.Marshal.Copy(_ptr, _pixels, 0, _size);

            // Unlock previously locked bits
            b.UnlockBits(_bmpData);
        }

        public _BitmapInfo() { }
        #endregion

        /// <summary>
        /// Copies pixels to existing bitmap. Make sure that _pixels array match to the bitmap
        /// (width, height, color depth etc).
        /// </summary>
        /// <param name="b"></param>
        /// <param name="ptr"></param>
        /// <returns></returns>
        public Bitmap CopyPixelsToBitmap()
        {
            BitmapData bmpData = _b.LockBits(
                new Rectangle(0, 0, _width, _height),
                ImageLockMode.ReadWrite, _b.PixelFormat);

            // Copy the RGB values back to the bitmap
            System.Runtime.InteropServices.Marshal.Copy(_pixels, 0, bmpData.Scan0, _size);

            // Unlock previously locked bits
            _b.UnlockBits(bmpData);

            return _b;
        }

        #region Private fields
        private Bitmap _b;

        private byte[] _pixels = null;

        /// <summary>Array of pixels</summary>
        public byte[] Pixels
        {
            get { return _pixels; }
            set { _pixels = value; }
        }

        private int _size = 0;

        /// <summary>Total count of bytes in image</summary>
        public int Size
        {
            get { return _size; }
            set { _size = value; }
        }

        private int _bytesPerPixel = 0;

        /// <summary>How many bytes each pixel takes</summary>
        public int BytesPerPixel
        {
            get { return _bytesPerPixel; }
            set { _bytesPerPixel = value; }
        }

        private int _stride = 0;

        /// <summary>Stride (bytes per row)</summary>
        public int Stride
        {
            get { return _stride; }
            set { _stride = value; }
        }

        private int _width = 0;

        /// <summary>Width of bitmap (in pixels)</summary>
        public int Width
        {
            get { return _width; }
            set { _width = value; }
        }

        private int _height = 0;

        /// <summary>Height of bitmap in pixels</summary>
        public int Height
        {
            get { return _height; }
            set { _height = value; }
        }
        #endregion

        #region IDisposable Members

        public void Dispose()
        {
            //
        }

        #endregion
    }
}
